package org.unisinos.gc.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.unisinos.gc.vo.Artigo;
import org.unisinos.gc.vo.Autor;
import org.unisinos.gc.vo.SubArea;

public class ArtigoDAO {

	public static List<Artigo> getArtigoList(int codigo) throws SQLException {
		List<Artigo> artigos = new ArrayList<Artigo>();
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			con = ConnectionManager.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery("select * from artigo where codigo_autor = " + codigo + " order by codigo");
			while(rs.next()) {
				Artigo c = new Artigo();
				c.setCodigo(rs.getInt("codigo"));
				c.setNome(rs.getString("nome"));
				c.setSubarea(new SubArea(null, rs.getInt("codigo_subarea"), null));
				c.setCriacao(rs.getDate("dt_criacao"));
				c.setResumo(rs.getString("resumo"));
				c.setResumoAbstract(rs.getString("abstract"));
				c.setAutor(AutorDAO.getAutor(rs.getInt("codigo_autor")));
				c.setUrlFile(rs.getString("url_file"));
				artigos.add(c);
			}
		} finally {
			try {
				if (con!=null)con.close();
				if (stmt!=null)stmt.close();
				if (rs!=null)rs.close();
			}catch (Exception e) {}
		}
		return artigos;
	}
	
	public static Artigo getArtigo(int codigo) throws SQLException {
		Artigo artigo = null;
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConnectionManager.getConnection();
			stmt = con.prepareStatement("select * from artigo where codigo = ?");
			stmt.setInt(1, codigo);
			rs = stmt.executeQuery();
			while(rs.next()) {
				artigo = new Artigo();
				artigo.setCodigo(rs.getInt("codigo"));
				artigo.setNome(rs.getString("nome"));
				artigo.setSubarea(new SubArea(null, rs.getInt("codigo_subarea"), null));
				artigo.setCriacao(rs.getDate("dt_criacao"));
				artigo.setResumo(rs.getString("resumo"));
				artigo.setResumoAbstract(rs.getString("abstract"));
				artigo.setAutor(AutorDAO.getAutor(rs.getInt("codigo_autor")));
				artigo.setUrlFile(rs.getString("url_file"));
			}
		} finally {
			try {
				if (con!=null)con.close();
				if (stmt!=null)stmt.close();
				if (rs!=null)rs.close();
			}catch (Exception e) {}
		}
		return artigo;
	}
	
	public static void addArtigo(Artigo artigo) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionManager.getConnection();
			ps = con.prepareStatement("INSERT INTO artigo(codigo, codigo_autor, codigo_subarea, " +
					"nome, dt_criacao, resumo,abstract, status, url_file) " +
					"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
			ps.setInt(1, artigo.getCodigo());
			ps.setInt(2, artigo.getAutor().getCodigo());
			ps.setInt(3, artigo.getSubarea().getCodigo());
			ps.setString(4, artigo.getNome());
			ps.setDate(5, new Date(artigo.getCriacao().getTime()));
			ps.setString(6, artigo.getResumo());
			ps.setString(7, artigo.getResumoAbstract());
			ps.setInt(8, artigo.getStatus());
			ps.setString(9, artigo.getUrlFile());
			
			ps.executeUpdate();
		} finally {
			try {
				if (con!=null)con.close();
				if (ps!=null)ps.close();
			}catch (Exception e) {}
		}
	}
	
	public static void deleteArtigo(int codigo) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionManager.getConnection();
			ps = con.prepareStatement("delete from artigo where codigo = ?");
			ps.setInt(1, codigo);
			ps.executeUpdate();
		} finally {
			try {
				if (con!=null)con.close();
				if (ps!=null)ps.close();
			}catch (Exception e) {}
		}
	}
	
	public static void updateArtigo(Artigo artigo) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionManager.getConnection();
			ps = con.prepareStatement("UPDATE artigo SET codigo_subarea=?, nome=?, " +
					"resumo=?, abstract=?, status=?, url_file=? " +
					"WHERE codigo = ?");
			ps.setInt(1, artigo.getSubarea().getCodigo());
			ps.setString(2, artigo.getNome());
			ps.setString(3, artigo.getResumo());
			ps.setString(4, artigo.getResumoAbstract());
			ps.setInt(5, artigo.getStatus());
			ps.setString(6, artigo.getUrlFile());
			ps.setInt(7, artigo.getCodigo());
			ps.executeUpdate();
		} finally {
			try {
				if (con!=null)con.close();
				if (ps!=null)ps.close();
			}catch (Exception e) {}
		}
	}
	
	public static Artigo getArtigoByNome(String nome) {
		Artigo artigo = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionManager.getConnection();
			ps = con.prepareStatement("select * from artigo where nome = ?");
			ps.setString(1, nome);
			rs = ps.executeQuery();
			while(rs.next()) {
				artigo = new Artigo();
				artigo.setCodigo(rs.getInt("codigo"));
				artigo.setNome(rs.getString("nome"));
				artigo.setSubarea(new SubArea(null, rs.getInt("codigo_subarea"), null));
				artigo.setCriacao(rs.getDate("dt_criacao"));
				artigo.setResumo(rs.getString("resumo"));
				artigo.setResumoAbstract(rs.getString("abstract"));
				artigo.setAutor(AutorDAO.getAutor(rs.getInt("codigo_autor")));
				artigo.setUrlFile(rs.getString("url_file"));
				break;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if (con!=null)con.close();
				if (ps!=null)ps.close();
				if (rs!=null)rs.close();
			}catch (Exception e) {}
		}
		return artigo;
	}
}